/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @headerfile shm.hpp
* 
* Header for easy comunication aplications through IPC(shared memory segment).
* 
* @author Miguel Ángel Vílchez López 2009 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _MOKARDU_SHM_HPP_
#define _MOKARDU_SHM_HPP_

#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include "debug.hpp"

/**
* @class CMShm
* @brief Comunication through shared memory segments.
*/

class CMShm
{

	private:

		/**
		* The log class.
		*/
		CMDebug debug;
		
		/**
		* the id who identifies the ipc.
		*/
		long int id;
		
		/**
		* The buffer size.
		*/
		unsigned int buffer_size;
		
		/**
		* Converts an array of chars (10 caracters max.) into numbers.
		*
		* @return Returns the number of the word.
		*/
		unsigned long int myatoi(const char *word);
	
	public:
	
		/**
		* Constructor
		*
		* @param[in] id The identificator who identifies the portion of memory shared.
		* @param[in] buffer The buffer size.
                * @param[in] defval The default value.
		* @param[in] debug If the debug is activated.
		* @param[in] filename The file for logging.
		*/
		CMShm(long int id, unsigned long int buffer,bool debug,const char* filename);
		
		/**
		* Destructor
		*/
		~CMShm();
		
		/**
		* Remove the shared memory segment.
		*
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int del();
		
		/**
		* Read data from a share memory segment.
		*
		* @param[out] data Variable to put data.
		* @return Returns the lenght of bytes wrotes if succesfull, -1 if not.
		*/
		int read(char *data);

		/**
		* Write data to a share memory segment.
		*
		* @param[in] data Variable data from.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int write(char *data);

};

#endif // _MOKARDU_SHM_HPP_
